#include "main.h"

int JudgmentPackageLength(FILE *pTsFile, int TsPosition)
{
    int Time = 0;
    int PackageByte = 0;
    int value = -1;

    if(fseek(pTsFile, TsPosition + 1, SEEK_SET) == -1)
    {
        printf("fseek error!\n");
    }

    for(Time = 0; Time < 10; Time++)
    {
        if(fseek(pTsFile, TsPosition - 1, SEEK_CUR) == -1)
        {   
            printf("fseek error!");

            return value;
        }

        if(feof(pTsFile))
        {
            value = -1;

            return value;
        }

        fseek(pTsFile,188,SEEK_CUR);
        PackageByte = fgetc(pTsFile);

        if(PackageByte == 0x47)
        {
            value = 188;
        }
        else
        {
            fseek(pTsFile,203,SEEK_CUR);
            PackageByte = fgetc(pTsFile);
            
            if(PackageByte == 0x47)
            {
                value = 204;
            }
            else
            {
                value = -1;
            }
        }
    }

    return value;
}

int ParseLen(FILE *pTsFile, int TsPosition)
{
    int PackageLength = 0;

    while(fgetc(pTsFile) != EOF)
    {
        if(fgetc(pTsFile) == 0x47)
        {
            if(JudgmentPackageLength(pTsFile, TsPosition) == 204)
            {
                PackageLength = 204;
                break;
            }

            if(JudgmentPackageLength(pTsFile, TsPosition) == 188)
            {
                PackageLength = 188;
                break;
            }
        }
    }

    return PackageLength;
}

